﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Drawing;
using System.Text;
using System.Threading.Tasks;
using Homework.Properties;

namespace Homework
{
    class Game
    {
        //说明游戏对象 背景
        private BackGround background;
        //说明游戏对象 玩家飞机
        private PlaneHero planehero;
        ////说明游戏对象 玩家子弹
        private BulletHero bullethero;
        //说明游戏对象 敌人飞机
        private PlaneEnemyo planeEnemyo;
        //说明游戏对象 敌人子弹
        private BulletEnemy bulletEnemy;
        //说明冷冻飞机
        private PlaneCold planecold;
        //说明冷冻子弹
        private BulletCold bulletcold;
        //说明糖果
        private Candy candy;
        
        //说明玩家子弹的集合
        private List<BulletHero> bulletHeroes { get; set; }
        //说明敌人飞机的集合
        private List<PlaneEnemyo> planeEnemyos { get; set; }  
        //说明敌人子弹的集合
        private List<BulletEnemy> bulletEnemies { get; set; }
        //说明被攻击的敌人飞机集合
        private List<PlaneEnemyo> planeEattacked { get; set; }
        //说明冷冻飞机的集合
        private List<PlaneCold> planeColds { get; set; }
        //说明冷冻子弹的集合
        private List<BulletCold> bulletColds { get; set; }
        //说明糖果的集合
        private List<Candy> candies { get; set; }

        
        //下一个敌人飞机的产生时间
        private DateTime nextplaneEnemy { get; set; }
        //说明下一个敌人子弹的产生时间
        private DateTime nextbulletEnemy { get; set; }
        //说明下一个冷冻飞机的产生时间
        private DateTime nextplaneCold { get; set; } 
        //说明下一个冷冻子弹的产生时间
        private DateTime nextbulletCold { get; set; }
        //说明被冻住的时间
        private DateTime timeforCold { get; set; }
        //说明下一个产生糖果的时间
        private DateTime nextcandy { get; set; }
        //定义糖果存在的时间
        private DateTime candyTime;

        //说明死亡的飞机的数量
        public int Enemydead = 0;
        public int Herodead = 0;
        //说明被冻住的布尔值
        public bool IsFrozen;
        public bool Tobefrozen;
        //定义出现的布尔值
        public bool candyShow = true;
        //初始化击中飞机的子弹
        BulletHero bulletHtouch = null;
        Candy Candyremove = null;

        //游戏自带一个时钟
        public System.Timers.Timer Timer
        {
            get;
            set;
        }
        
        //定义委托
        public delegate void GameChanged();
        public delegate void GameOver();
        //定义事件
        public event GameChanged gameChanged;
        public event GameOver gameOver;

        public Game()
        {
            //初始化游戏对象 背景
            background = new BackGround(new Rectangle(0,-631,734,1262));
            //初始化游戏对象 玩家飞机
            planehero = new PlaneHero(new Rectangle(335,540,68,92),15);            
            //初始化玩家子弹集合
            bulletHeroes = new List<BulletHero>();
            //初始化敌人飞机集合
            planeEnemyos = new List<PlaneEnemyo>();
            //初始化敌人子弹集合
            bulletEnemies = new List<BulletEnemy>();
            //初始化被攻击敌人飞机集合
            planeEattacked = new List<PlaneEnemyo>();
            //初始化冷冻飞机集合
            planeColds = new List<PlaneCold>();
            //初始化冷冻子弹集合
            bulletColds = new List<BulletCold>();
            //初始化糖果集合
            candies = new List<Candy>();
            //初始化时钟
            Timer = new System.Timers.Timer();
            //插入游戏自带的时钟
            Timer.Elapsed += Timer_Elapsed;
            //时钟开始作用
            Timer.Start();
        }

        /// <summary>
        /// 时钟处理事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void Timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            //时钟暂停
            Timer.Stop();
            //时钟处理程序
            background.Move();
            //玩家子弹移动            
            foreach (var bullethero in bulletHeroes)
            {
                bullethero.Move();
            }
            //移除出界类
            bulletHeroes.RemoveAll(m => m.rectangle.Y <= -630);

            //敌人飞机移动            
            if (Tobefrozen==false)
            {
                foreach (var planeEnemyo in planeEnemyos)
                {
                    planeEnemyo.Move();
                }
            }
            //移除出界类
            planeEnemyos.RemoveAll(m=>m.rectangle.Y>=1000);

            //敌人子弹移动            
            if (Tobefrozen==false)
            {
                foreach (var bulletEnemy in bulletEnemies)
                {
                    bulletEnemy.Move();
                }
            }
            //移除出界类
            bulletEnemies.RemoveAll(m=>m.rectangle.Y>=1000);

            //冷冻飞机的移动
            if (Tobefrozen==false)
            {
                foreach (var planecold in planeColds)
                {
                    planecold.Move();
                }
            }
            //移除出界的飞机
            planeColds.RemoveAll(m => m.rectangle.Y >= 1000);

            //冷冻子弹的移动
            if (Tobefrozen==false)
            {
                foreach (var bulletcold in bulletColds)
                {
                    bulletcold.Move();
                }
            }
            //移除出界子弹
            bulletColds.RemoveAll(m => m.rectangle.Y >= 1000);

            //处理敌人飞机随机出现
            if (System.DateTime.Now > nextplaneEnemy)
            {
                
                //产生随机数 用于敌人飞机出现的横坐标
                Random rnd = new Random();
                int i = rnd.Next(50, 650);
                //初始化游戏对象 敌人飞机
                Rectangle recplaneEnemy = new Rectangle(i, 0, 68, 92);
                planeEnemyo = new PlaneEnemyo(recplaneEnemy,5);
                if (Tobefrozen==false)
                {
                    //加入新敌机
                    planeEnemyos.Add(planeEnemyo);
                }
                //每过6秒产生一个新敌机
                nextplaneEnemy = System.DateTime.Now.AddSeconds(6);
            }

            //处理敌人子弹发出
            if (System.DateTime.Now>nextbulletEnemy)
            {
                for (int i = 0; i < planeEnemyos.Count; i++)
                {
                    //初始化敌人子弹
                    Rectangle recbulletEnemy = new Rectangle(planeEnemyos[i].rectangle.X + planeEnemyos[i].rectangle.Width / 2 - 5, planeEnemyos[i].rectangle.Y + planeEnemyos[i].rectangle.Height - 5, 10, 27);
                    bulletEnemy = new BulletEnemy(recbulletEnemy);
                    if (Tobefrozen==false)
                    {
                        //将初始化后的敌人子弹加入集合
                        bulletEnemies.Add(bulletEnemy);
                    }
                    //每过2.5秒产生一个敌人子弹
                    nextbulletEnemy = System.DateTime.Now.AddSeconds(2.2);
                }                
            }

            //处理冷冻飞机随机出现
            if (System.DateTime.Now > nextplaneCold)
            {
                //产生横坐标的随机数
                Random rnd = new Random();
                int i = rnd.Next(50, 650);
                if (i!=planeEnemyo.rectangle.X)
                {
                    //初始化游戏对象 冷冻飞机
                    Rectangle recplanecold = new Rectangle(i, 0, 68, 92);
                    planecold = new PlaneCold(recplanecold, 5);
                    if (Tobefrozen==false)
                    {
                        //加入冷冻集合
                        planeColds.Add(planecold);
                    }
                    //每过9秒产生一个新的冷冻飞机
                    nextplaneCold = System.DateTime.Now.AddSeconds(9.5);
                }

            }
            //处理冷冻子弹发出
            if (System.DateTime.Now>nextbulletCold)
            {
                for (int i = 0; i < planeColds.Count; i++)
                {
                    if (planeColds[i] != null)
                    {
                        //初始化冷冻子弹
                        Rectangle recbulletCold = new Rectangle(planeColds[i].rectangle.X + planeColds[i].rectangle.Width / 2 - 5, planeColds[i].rectangle.Y + planeColds[i].rectangle.Height - 5, 10, 27);
                        bulletcold = new BulletCold(recbulletCold);
                        if (Tobefrozen==false)
                        {
                            //加入新子弹
                            bulletColds.Add(bulletcold);
                        }
                        //过5秒产生一个新子弹
                        nextbulletCold = System.DateTime.Now.AddSeconds(3.5);
                    }
                }
            }
            //处理冷冻事件
            if (System.DateTime.Now>timeforCold)
            {
                if (IsFrozen==true)
                {
                    Tobefrozen = true;
                    IsFrozen = false;
                    timeforCold = System.DateTime.Now.AddSeconds(5);
                }
                else
                {
                    Tobefrozen = false;
                }
                
            }
            //处理糖果的随机出现
            if (System.DateTime.Now > nextcandy)
            {

                //产生随机数 用于糖果出现的横坐标
                Random rnd = new Random();
                int i = rnd.Next(50, 650);
                int j = rnd.Next(30, 600);
                //初始化游戏对象 糖果
                Rectangle recCandy = new Rectangle(i, j, 37, 18);
                candy = new Candy(recCandy);
                if (Tobefrozen == false)
                {
                    //加入新糖果
                    candies.Add(candy);
                }
                //每过12秒产生一个新糖果
                nextcandy = System.DateTime.Now.AddSeconds(12);
            }
            //处理糖果出现时间
            if (System.DateTime.Now>candyTime)
            {
                if (candyShow==false)
                {
                    for (int i = 0; i < candies.Count; i++)
                    {
                        Candyremove = candies[i];
                    }
                    candyShow = true;                    
                }
                else
                {
                    candyShow = false;
                }
                candyTime = System.DateTime.Now.AddSeconds(7);
            }


            //移除死亡飞机
            planeEnemyos.RemoveAll(m => m.PlaneEnemyolifenow <= 0);
            planeColds.RemoveAll(m => m.PlaneColdlifenow <= 0);
            //移除糖果
            if (Candyremove!=null)
            {
                candies.Remove(Candyremove);
            }
            //处理碰撞
            Attack();
            //激发事件
            if (gameChanged!=null)
            {
                gameChanged();
            }
            if (gameOver!=null)
            {
                gameOver();
            }
            //时钟开始
            Timer.Start();
        }
        /// <summary>
        /// 处理碰撞事件
        /// </summary>
        private void Attack()
        {
            //初始化击中飞机的子弹
            BulletEnemy bulletEtouch = null;
            BulletCold bulletCtouch = null;
            //处理敌人子弹攻击玩家飞机
            for (int i = 0; i < bulletEnemies.Count; i++)
            {                
                    if (bulletEnemies[i].rectangle.IntersectsWith(planehero.rectangle))
                    {
                        //移入击中的子弹集合
                        bulletEtouch = bulletEnemies[i];
                        //被击中的玩家飞机生命值减少
                        planehero.PlaneHerolifenow--;
                        //闪烁的布尔值变为True
                        planehero.HeroChanged = true;
                        //处理死亡玩家飞机
                        if (planehero.PlaneHerolifenow<=0)
                        {
                            //计数
                            Herodead++;
                        }
                    }                               
            }
            //冷冻子弹
            for (int p = 0; p < bulletColds.Count; p++)
            {
                if (bulletColds[p].rectangle.IntersectsWith(planehero.rectangle))
                {
                    //移入击中的子弹集合
                    bulletCtouch = bulletColds[p];
                    //被击中的玩家飞机生命值减少
                    planehero.PlaneHerolifenow--;
                    //闪烁的布尔值变为True
                    planehero.HeroChanged = true;
                    if (planehero.PlaneHerolifenow <= 0)
                    {
                       //计数
                        Herodead++;
                    }
                }
            }

            //处理玩家子弹攻击敌人飞机
            for (int j = 0; j < bulletHeroes.Count; j++)
            {
                for (int k = 0; k < planeEnemyos.Count ; k++)
                {
                        //敌人飞机
                        if (bulletHeroes[j].rectangle.IntersectsWith(planeEnemyos[k].rectangle))
                        {
                            //移入击中的子弹集合
                            bulletHtouch = bulletHeroes[j];
                            //被击中的敌人飞机生命值减小
                            planeEnemyos[k].PlaneEnemyolifenow--;
                            // 闪烁的布尔值为True
                            planeEnemyos[k].Enemychanged = true;
                            //处理死亡敌人飞机
                            if (planeEnemyos[k].PlaneEnemyolifenow <= 0)
                            {                               
                                //计数
                                Enemydead++;
                            }
                        }                    
                }
                for (int m = 0; m < planeColds.Count; m++)
                {
                    //冷冻飞机
                    if (bulletHeroes[j].rectangle.IntersectsWith(planeColds[m].rectangle))
                    {
                        //移入击中子弹集合
                        bulletHtouch = bulletHeroes[j];
                        //被击中后生命值减小
                        planeColds[m].PlaneColdlifenow--;
                        //改变闪烁布尔值
                        planeColds[m].Coldchanged = true;
                        //处理死亡飞机
                        if (planeColds[m].PlaneColdlifenow <= 0)
                        {
                            //计数
                            Enemydead++;
                        }
                    }
                }
            }           

            //移除击中的子弹
            if (bulletHtouch!= null)
            {
                bulletHeroes.Remove(bulletHtouch);
            }
            if (bulletEtouch!=null)
            {
                bulletEnemies.Remove(bulletEtouch);
            }
            if (bulletCtouch!=null)
            {
                bulletColds.Remove(bulletCtouch);
            }
            //处理敌人飞机和玩家飞机的碰撞
            for (int l = 0; l < planeEnemyos.Count; l++)
            {
                for (int n = 0; n < planeColds.Count; n++)
                {
                    if (planeEnemyos[l].rectangle.IntersectsWith(planehero.rectangle))
                    {
                        //碰撞后均死亡
                        planehero.PlaneHerolifenow = 0;
                        planeEnemyos[l].PlaneEnemyolifenow = 0;
                        //计数
                        Herodead++;
                    }
                    else if (planeColds[n].rectangle.IntersectsWith(planehero.rectangle))
                    {
                        //碰撞后均死亡
                        planehero.PlaneHerolifenow = 0;
                        planeColds[n].PlaneColdlifenow = 0;
                        //计数
                        Herodead++;
                    }
                }
            }
            //处理糖果和玩家飞机的碰撞
            for (int i = 0; i < candies.Count; i++)
            {
                if (planehero.PlaneHerolifenow<15)
                {
                    if (candies[i].rectangle.IntersectsWith(planehero.rectangle))
                    {
                        planehero.PlaneHerolifenow++;
                        Candyremove = candies[i];
                    }
                }
            }
        }
        /// <summary>
        /// 绘制函数
        /// </summary>
        /// <param name="graphics"></param>
        /// <param name="size"></param>
        public void Draw(Graphics graphics,Size size)
        {
            background.Draw(graphics);
            if (planehero!=null && Herodead==0)
            {
                planehero.Draw(graphics);
            }
            //玩家子弹          
            for (int i = 0; i < bulletHeroes.Count; i++)
            {
                bulletHeroes[i].Draw(graphics);

            }
            //敌人飞机            
            for (int i = 0; i < planeEnemyos.Count; i++)
            {
                if (planeEnemyos[i]!=null)
                {
                    planeEnemyos[i].Draw(graphics);                    
                }
            }
            //敌人子弹
            for (int i = 0; i < bulletEnemies.Count; i++)
            {
                if (bulletEnemies[i]!=null)
                {
                    bulletEnemies[i].Draw(graphics);
                }
            }
            //冷冻飞机
            for (int i = 0; i < planeColds.Count; i++)
            {
                if (planeColds[i]!=null)
                {
                    planeColds[i].Draw(graphics);
                }
            }
            //冷冻子弹
            for (int i = 0; i < bulletColds.Count; i++)
            {
                if (bulletColds[i]!= null)
                {
                    bulletColds[i].Draw(graphics);
                }
            }
            //糖果
            for (int i = 0; i <candies.Count; i++)
            {
                if (candies[i]!=null)
                {
                    candies[i].Draw(graphics);
                }
            }
        }

        //在panelright区域画
        public void Drawright(Graphics graphics)
        {
            //画血条
            Brush lifeEBrush = new SolidBrush(Color.OldLace);
            //画攻击血条
            SolidBrush attackedBrush = new SolidBrush(Color.DarkSalmon);
            //画将死血条
            SolidBrush deadBrush = new SolidBrush(Color.IndianRed);          
            graphics.FillRectangle(lifeEBrush, 8, 105, 180, 13);
            if (planehero.PlaneHerolifenow > 3 && planehero.PlaneHerolifenow < 15)
            {
                graphics.FillRectangle(attackedBrush, 8, 105, planehero.PlaneHerolifenow * 12, 13);
            }
            else if (planehero.PlaneHerolifenow < 4 && planehero.PlaneHerolifenow>0)
            {
                graphics.FillRectangle(deadBrush, 8, 105, planehero.PlaneHerolifenow * 12, 13);
            }
            //画得分
            Font drawFont = new Font("transparant", 16);
            SolidBrush drawBrush = new SolidBrush(Color.OldLace);
            graphics.DrawString(Enemydead.ToString(), drawFont, drawBrush, 10, 32);
            //画生命值
            Font drawpoint = new Font("transparant", 14);
            SolidBrush pointBrush = new SolidBrush(Color.OldLace);
            graphics.DrawString(planehero.PlaneHerolifenow.ToString(), drawFont, drawBrush, 156, 75);
        }
        
        /// <summary>
        /// 键盘事件
        /// </summary>
        /// <param name="key"></param>
        public void KeyDown(String key)
        {
            //调用子类的处理键盘
            planehero.KeyDown(key);
            //调用子类的移动函数
            planehero.Move();
            if (key=="K")
            {
                //初始化游戏对象
                Rectangle recbullethero = new Rectangle(planehero.rectangle.X + planehero.rectangle.Width / 2 - 6, planehero.rectangle.Y, 10, 27);
                bullethero = new BulletHero(recbullethero);
                bulletHeroes.Add(bullethero);
            }
            
        }

        //用得分替换玩家飞机生命值
        public void ButtonAdd()
        {
            if (Enemydead>=5 && planehero.PlaneHerolifenow<=15)
            {
                planehero.PlaneHerolifenow++;
                Enemydead = Enemydead - 5;
            }
        }       

        //用得分替换使敌人部分静止
        public void ButtonFrozen()
        {
            if (Enemydead>=5)
            {
                Enemydead = Enemydead - 5;
                IsFrozen = true;
            }
        }
    }
}
